package com.unisal.java;


import java.util.Iterator;
import java.util.Stack;

public class IteradorComposto implements Iterator {
	Stack fila = new Stack();
	
	public IteradorComposto(Iterator iterador) {
		fila.push(iterador);
	}

	@Override
	public boolean hasNext() {
		if (fila.empty()){
			return false;
		} else {
			Iterator iterator = (Iterator) fila.peek();
			if (!iterator.hasNext()){
				fila.pop();
				return hasNext();
			} else {
				return true;
			}
		}
	}

	@Override
	public Object next() {
		if (hasNext()){
			Iterator iterador = (Iterator) fila.peek();
			MenuComponente componente = (MenuComponente) iterador.next();
			if (componente instanceof Menu){
				fila.push((componente).criaIterador());
			}
			return componente;
		} else {
			return null;
		}
		
	}

	@Override
	public void remove() {
		throw new UnsupportedOperationException(); 

	}

}
